#ifndef THREADMANAGER_H
#define THREADMANAGER_H

#include <QThread>
#include <QObject>
#include <QRgb>
#include <QImage>
#include <QPainter>
#include <windows.h>
#include <imgwidget.h>
#include <QDebug>

class ThreadManager;

class DataReader:public QObject {
    Q_OBJECT

public:
    ThreadManager* parent;
    uchar dataBuffer[1544];
    bool readDone;
    DataReader(ThreadManager* p):parent(p),readDone(false){}
public slots:
    void readPortData();
signals:
    void reader2processor_readDone();
};

class DataProcessor:public QObject {
    Q_OBJECT
public:
    DataProcessor(ThreadManager* p):
        parent(p),isRunning(false),matAvailbale(true){}
    ThreadManager* parent;
    bool isRunning;
    bool matAvailbale;
    uchar dataBuffer[1544];
    uint16_t dataFrame[768];
    float mlx90640To[768];
    QRgb pixels[image_param::imageWidth*image_param::imageHeight];

    float mat[24][32];
    DWORD matRGB[24][32][3];
#define newHeight  24*3
#define newWidth  32*3
#define resolution  3
    float img[newHeight][newWidth];
    DWORD imgRGB[newHeight][newWidth][3];
    DWORD outImgRGB[newHeight][newWidth];
public slots:
    void processData();
signals:
    void processor2manager_draw();
    void processor2reader_read();
};

class ThreadManager:public QObject {
    Q_OBJECT

public:
    QThread dataProcessThread;
    QThread dataReadThread;
    DataProcessor* processor;
    DataReader* reader;


    /*data*/
    HANDLE port;
    IMGWIDGET* imgwidget;
    double emmity;
    bool writeDone;
    bool isSettingData;


    ThreadManager():port(HANDLE(-1)),writeDone(true),isSettingData(false) {
        processor = new DataProcessor(this);
        reader = new DataReader(this);
        processor->moveToThread(&dataProcessThread);
        reader->moveToThread(&dataReadThread);

        connect(this, &ThreadManager::manager2reader_read, reader, &DataReader::readPortData, Qt::AutoConnection);
        connect(reader, &DataReader::reader2processor_readDone, processor, &DataProcessor::processData, Qt::AutoConnection);
        connect(processor,&DataProcessor::processor2manager_draw, this, &ThreadManager::draw, Qt::AutoConnection);
        //connect(processor,&DataProcessor::processor2reader_read,reader,&DataReader::readPortData,Qt::AutoConnection);

        //initialize
        for (int i=0;i<image_param::imageHeight;i++){
            for (int j=0;j<image_param::imageWidth;j++){
                this->processor->pixels[i*image_param::imageWidth+j] = static_cast<QRgb>(0x00ffffff);
            }
        }
    }
    ~ThreadManager() {
        delete processor;
        delete reader;
    }
public slots:
    /* 开始线程之前应该由主窗口负责把manager的各data变量同步 */
    void startThread() {
        dataReadThread.start();
        dataProcessThread.start();
        emit manager2reader_read();
    }
    void stopThread() {
        // qDebug()<<"reader quit";
        // dataReadThread.quit();
        dataReadThread.terminate();
        // qDebug()<<"reader wait";
        dataReadThread.wait();
        // Sleep(200);
        // qDebug()<<"processor quit";
        // dataProcessThread.quit();
        dataProcessThread.terminate();
        // qDebug()<<"processor wait";
        dataProcessThread.wait();
        // Sleep(200);
    }
    void draw() {
        processor->isRunning = false;
        imgwidget->repaint();
    }
signals:
    /* 管理器告诉读者要读数据了 */
    void manager2reader_read();
};



#endif // THREADMANAGER_H
